/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.extns.bluetooth;

import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;

/**
 * Bluetooth client
 * 
 * @author kentelt
 *@see com.aphrodite.extns.bluetooth.BTUtil.connect
 */
public final class BTClient {

	/**
	 * The client session with the server
	 */
	private ClientSession clientSession;

	public BTClient(String serviceURL) {
		// Try to open the connection
		try {
			clientSession = (ClientSession) Connector.open(serviceURL);
			clientSession.connect(null);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Close the client communication with the server
	 */
	public void close() {
		try {
			// If the client conexion is open disconnect first
			// then close the connection
			if (clientSession != null) {
				clientSession.disconnect(null);
				clientSession.close();
				clientSession = null;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * Put the object
	 * 
	 * @param obj
	 * @return reponse code. -1 if the operation has failed
	 */
	public int put(BTData obj) {
		// Server response, if exception -1
		int responseCode = -1;

		try {
			// Create the header set with the object information
			HeaderSet headerSet = clientSession.createHeaderSet();
			headerSet.setHeader(HeaderSet.NAME, obj.name);
			headerSet.setHeader(HeaderSet.TYPE, obj.type);
			headerSet.setHeader(HeaderSet.LENGTH, new Long(obj.data.length));

			// Set the PUT operation
			Operation operation = clientSession.put(headerSet);

			// Write data
			DataOutputStream dataOutputStream = operation.openDataOutputStream();
			dataOutputStream.write(obj.data);
			dataOutputStream.close();

			// Get the response from the server
			responseCode = operation.getResponseCode();

			// Close the operation
			operation.close();
		} catch (IOException e) {

		}

		// Return the response code
		return responseCode;
	}

	/**
	 * Put binary data on the obex server
	 * 
	 * @param data
	 *            The binary data to send
	 */
	public int put(byte[] data) {
		// Create a object without name, binary type and bytes
		BTData mObexObject = new BTData(Obex.NO_NAME, Obex.TYPE_BINARY, data);

		// Put the object
		return put(mObexObject);
	}

	/**
	 * Put a string in the server
	 * 
	 * @param text
	 *            The text to send
	 */
	public int put(String text) {
		// Get the bytes of the text
		byte[] data = text.getBytes();

		// Create an object without name, text type and bytes
		BTData mObexObject = new BTData(Obex.NO_NAME, Obex.TYPE_TEXT, data);

		// Put the object
		return put(mObexObject);
	}

}
